package framework.spring.security;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

public class BoenUserImpl extends JdbcDaoSupport implements UserDetailsService {

	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException {
		UserDetails userDetails = (UserDetails) getJdbcTemplate().queryForObject(
						"select username,password from users where username = ?",
						new String[] { username },
						new RowMapper<UserDetails>() {
	@Override
							public UserDetails mapRow(ResultSet rs, int arg1) throws SQLException {
								List<GrantedAuthority> list = new ArrayList<GrantedAuthority>();
								list.add(new SimpleGrantedAuthority("ROLE_MANAGER"));
								list.add(new SimpleGrantedAuthority("ROLE_ALL"));
								String username = rs.getString("username");
								String password = rs.getString("password");
								return new User(username, password, true, true,true, true, list);
			}
			
		});
		System.out.println("login user is :" + username);
		return userDetails;
	}

}
